﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;

namespace KeyGen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox_ApplicationList.Items.Add("QZoneBrowser QQ空间访问器");
            comboBox_ApplicationList.Items.Add("QZoneBrowser2");
            comboBox_ApplicationList.Items.Add("QZoneBrowser3");
            comboBox_ApplicationList.Items.Add("QZoneBrowser4");
            comboBox_ApplicationList.Items.Add("QZoneBrowser5");
        }

        private void button_GenerateCode_Click(object sender, EventArgs e)
        {
            bool selected = true ;
            string machineCode = textBox_MachineCode.Text;
            if (comboBox_ApplicationList.SelectedIndex == -1)
            {
                selected = false;
                MessageBox.Show("请选择一个应用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (textBox_MachineCode.Text == "")
            {
                selected = false;
                MessageBox.Show("请输入机器码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


            if (selected)
            {
                switch (comboBox_ApplicationList.SelectedIndex)
                {
                    case 0: //QZoneBrowser QQ空间访问器
                        textBox_RegistrationCode.Text = DCB001(MD5(machineCode));;
                        break;
                    case 1:
                        textBox_RegistrationCode.Text = "1";
                        break;
                    case 2:
                        textBox_RegistrationCode.Text = "2";
                        break;
                    case 3:
                        textBox_RegistrationCode.Text = "3";
                        break;
                    default:
                        textBox_RegistrationCode.Text = "未知应用！";
                        break;
                }
            }          
        }

        #region algorithm
        public static string SHA1(string source)
        {
            return (source != "") ? (FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1")) : "";
        }
        public static string MD5(string source)
        {
            return (source != "") ? (FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5")) : "";
        }
        public static string DCB001(string source)
        {
            List<string> mList = new List<string>();
            for (int i = 0; i < source.Length; i += 3)
            {
                string x = "";
                x = source.Substring(i, 1);
                if (i == 3 * 1)
                {
                    mList.Add("B");
                }
                else if (i == 3 * 5)
                {
                    mList.Add("5");
                }
                else if (i == 3 * 9)
                {
                    mList.Add("A");
                }
                else
                {
                    mList.Add(x);
                }
            }
            return string.Join("", mList.ToArray());
        }
        #endregion
    }
}
